<?php 

namespace App\Factories;

use Permission;
use Validator;
use Request;
use Session;

class RbacFactory extends Factory{

	// 新增角色

	public function addRole( $data ){

		$attrName = [
			'name'=>"角色代号",
			'display_name' => "角色名",
			'description'  => "描述",
		];

		$this->validate($data, [
		    'name' => 'required|max:50',
		    'display_name' => 'required|max:50',
		    'description'  => 'required|max:255',
		],$attrName);

		$role = new \App\Models\Role;
		
		$has = $role->where(['name' => $data['name']])->get();
		if($has){
			return self::result(0,"角色：'{$data['name']}'已存在");
		}

		return $role->fill($data)->save();
	
	}


	// 分配角色
	public function attachRole( $userId , $roleId ){
		$user = Model("user")->find($userId);

		if(!$user){
			return self::result(0,"用戶不存在");
		}

		$role = Model("role")->find($roleId);
		if(!$role){
			return self::result(0,"角色不存在");
		}

		$hasRole = 		$user->roles()->first();

		if($hasRole){
			return self::result(0,"请先删除用户已存在角色");
		}

		$ret = $user->attachRole($role);

		return $ret;

	}


	// 获取用户角色
	public function getUserRole( $userId ){
		$attrName = [
			'userId'=>"用户ID",
		];

		$this->validate(['userId' => $userId], [
			'userId' => 'required|integer',
		],$attrName);

		$user = Model("user")->find($userId);
		if(!$user){
			return self::result(0,"用戶不存在");
		}

		$role = $user->roles()->first();

		return $role;
	}


	// 清楚用户角色
	public function cleanRole($userId){
		$user = Model("user")->find($userId);

		if(!$user){
			return self::result(0,"用戶不存在");
		}

		return $user -> detachRoles();
	}

	// 新增权限
	public function addPermission( $data ){
		$attrName = [
			'name'=>"权限代号",
			'display_name' => "权限名",
			'description'  => "描述",
		];

		$this->validate($data, [
			'name' => 'required|max:50',
			'display_name' => 'required|max:50',
			'description'  => 'required|max:255',
		],$attrName);

		$permission = new \App\Models\Permission;

		$has = $permission->where(['name' => $data['name']])->first();

		if($has){
			return self::result(0,"权限代号：'{$data['name']}'已存在");
		}

		return $permission->fill($data)->save();
	}

	// 分配角色权限

	public function updatePermissions( $roleId , Array $permission  ){

		if(!is_array($permission)){
			return false;
		}

		$role = Model("role")->find($roleId);
		if(!$role){
			return false;
		}

		$hasPermission = Model("Permission")->getRolePermissions($role->id);

		$attachPermission = array_diff($permission,$hasPermission);
		$detachPermission = array_diff($hasPermission,$permission);


		//角色已经拥有要添加的权限
		if(!$attachPermission && !$detachPermission){
			return self::result(0,"角色权限无变化");
		}

		$role->attachPermissions($attachPermission);

		$role->detachPermissions($detachPermission);

		return true;

	}

	// 获取角色权限
	public function getRolePermissions( $roleId ){
		
		$role = Model("role")->find($roleId);
		if(!$role){
			return self::result(0,"未找到角色");
		}

		return Model("Permission")->getRolePermissions($role->id);
	}


	// 验证用户权限
	public static function can( $psermission ){
		
		$user = Model("User")->find(Session::get("user_info")['id']);

		if(!Session::get("user_info")['id'] || !$user){
			return self::result(0,"用戶不存在");
		}

		if($user->name == "admin"){
			return self::result(1,"success");
		}

		if(!$user->can($psermission)){
			return self::result(0,"用户没有改功能权限");
		}
		
	}

}
?>